//
//  ReadSelectVC.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ReadSelectVC.h"

@interface ReadSelectVC ()<UMSocialUIDelegate>
@property (nonatomic, copy)NSString *url;

// 分享标题
@property (nonatomic, copy)NSString *shareTitle;
@property (nonatomic, assign)BOOL isT;
@end

@implementation ReadSelectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];
    // 分享
    UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    // 收藏
    UIButton *shoucang = [UIButton buttonWithType:UIButtonTypeCustom];
    [shoucang setImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
    [shoucang setImage:[UIImage imageNamed:@"shoucanghou"] forState:UIControlStateSelected];
    shoucang.selected = NO;
    shoucang.frame = CGRectMake(WIDTH * 300, 0, 30 * WIDTH, 30 * HEIGHT);
//    shoucang.backgroundColor = [UIColor blueColor];
    [shoucang addTarget:self action:@selector(shoucang:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *shoucangbar = [[UIBarButtonItem alloc]initWithCustomView:shoucang];
    self.navigationItem.rightBarButtonItems = @[shoucangbar, share];

    
    UIWebView *wv = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:wv];
    [wv release];
    
    // 加载网址
    self.url = self.str;
    self.name = self.urlTitle;
    self.shareTitle = self.urlTitle;
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
        
}
- (void)shareAction
{
    // 507fcab25270157b37000010
    //56a87642e0f55ae402002a71
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56a87642e0f55ae402002a71"
                                      shareText:[_shareTitle stringByAppendingString:_str]
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil]
                                       delegate:self];
    
}
// 收藏
- (void)shoucang:(UIButton *)sc
{
    // 创建单例对象
    ReadDBManager *dbManager = [ReadDBManager shareInstance];
    // 打开数据库
    [dbManager openDB];
    // 创建表
    [[ReadDBManager shareInstance] createTable];
    // 添加数据
    ReadModel *rr = [[ReadModel alloc] init];
    rr.title = self.urlTitle;
    rr.thumb = self.str;

    _isT = !_isT;
    if (_isT) {
        sc.selected = YES;
        NSMutableArray *shareArr = [[ReadDBManager shareInstance] select];
        for (ReadModel *rr in shareArr) {
            if ([rr.title isEqualToString:_urlTitle]) {
                NSLog(@"已经收藏");
                return;
            }
        }
        UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        // 弹窗出现
        [self presentViewController:alert animated:YES completion:nil];
        [self performSelector:@selector(diss) withObject:alert afterDelay:1];
        [[ReadDBManager shareInstance] insertRead:rr];
        
        
    }else{
        sc.selected = NO;
        UIAlertController *bbAlert =[UIAlertController alertControllerWithTitle:@"提示" message:@"取消收藏" preferredStyle:UIAlertControllerStyleAlert];
        // 弹窗出现
        [self presentViewController:bbAlert animated:YES completion:nil];
        [self performSelector:@selector(diss) withObject:bbAlert afterDelay:1];
        [[ReadDBManager shareInstance] deleteReadWithtitle:rr.title];
        
    }
}
- (void)diss
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
